package com.lb.videoplay.fragment;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.lb.videoplay.R;
import com.lb.videoplay.adapter.BaseFragmentPagerAdapter;
import com.qmuiteam.qmui.widget.QMUITabSegment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr.lb
 * 时间： 2018/11/15
 * 包名： com.lb.videoplay.activity
 * 描述： Home Fragment
 */
public class HomeFragment extends Fragment {

    private TextView mCentent;
    private QMUITabSegment mTabSegment;
    private ViewPager mContentViewPager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //通过参数中的布局填充获取对应布局
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initWidget(view);
        initTitle();
        initData();
        initTabAndPager();
        return view;
    }

    public void initTitle() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getActivity().getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                );
            }
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            //window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }
    //初始化控件
    private void initWidget(View view){
        mCentent=view.findViewById(R.id.center_title);
        mTabSegment=view.findViewById(R.id.tab_segment);
        mContentViewPager=view.findViewById(R.id.content_view_pager);
    }

    //初始化
    private void initData(){
        mCentent.setText(getString(R.string.app_name));
    }

    //初始化Tab
    private void initTabAndPager() {
        List<Fragment> fragments = new ArrayList<>();
        FeaturedFragment mFeaturedFragment = new FeaturedFragment();
        AssortFragment mAssortFragment = new AssortFragment();

        fragments.add(mFeaturedFragment);
        fragments.add(mAssortFragment);

        BaseFragmentPagerAdapter adapter = new BaseFragmentPagerAdapter(getActivity().getSupportFragmentManager(), fragments, true);
        mContentViewPager.setAdapter(adapter);

        mTabSegment.setMode(QMUITabSegment.MODE_FIXED);  //MODE_SCROLLABLE 自适应宽度+滚动   MODE_FIXED  均分
        mTabSegment.setHasIndicator(true);
        mTabSegment.setIndicatorPosition(false);
        mTabSegment.setIndicatorWidthAdjustContent(true);
        // mTabSegment.addTab(new QMUITabSegment.Tab("    "+getText(R.string.app_me_income)+"    "));
        // mTabSegment.addTab(new QMUITabSegment.Tab("    "+getText(R.string.app_me_expend)+"    "));

        QMUITabSegment.Tab component = new QMUITabSegment.Tab(
                null,
                null,
                getText(R.string.featured), true
        );
        component.setTextColor(getResources().getColor(R.color.baseres_size_color_one_title), getResources().getColor(R.color.baseres_bt_color));
        QMUITabSegment.Tab util = new QMUITabSegment.Tab(
                null,
                null,
                getText(R.string.assort), true
        );
        util.setTextColor(getResources().getColor(R.color.baseres_size_color_one_title), getResources().getColor(R.color.baseres_bt_color));

        mTabSegment.addTab(component);
        mTabSegment.addTab(util);
        mTabSegment.setupWithViewPager(mContentViewPager, false);
        mTabSegment.addOnTabSelectedListener(new QMUITabSegment.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int index) {
                mTabSegment.hideSignCountView(index);
            }

            @Override
            public void onTabUnselected(int index) {

            }

            @Override
            public void onTabReselected(int index) {
                mTabSegment.hideSignCountView(index);
            }

            @Override
            public void onDoubleTap(int index) {

            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
    }

}